/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package axil.vm;

import axil.api.AxilObject;
import axil.api.Script;
import axil.api.error.AxilException;
import axil.api.extend.Environment;
import axil.vm.opcode.Opcode;
import axil.vm.stack.StackFrame;

import java.util.List;


/**
 * An Axil script as an executable object. This implements the Axil native API
 * Script interface.
 */
public class Executable implements Script {
	private String name;
	private List<String> references;
	private Opcode code;


	public Executable(String name, List<String> references, Opcode code) {
		this.name = name;
		this.references = references;
		this.code = code;
	}


	/**
	 * Return the name of the script. The name is the name given when the
	 * script was compiled.
	 *
	 * @return
	 * 	Returns the script name. The string returned is never null.
	 */
	public String name() {
		return name;
	}


	public void name(String name) {
		this.name = name;
	}


	public Opcode code() {
		return code;
	}


	/**
	 * Provide a list of recognized host application objects accessed by this
	 * script. This method does not definitively find all possible objects, only
	 * those that are explicitly referenced. For example:
	 * <pre>
	 *     let(order, find-order(12345), sum(order.items))
	 * </pre>
	 * In this example, the fictitious find_order() function returns a host
	 * application Order object, but Axil is unable to determine this. Or, this:
	 * <pre>
	 *     inject(order.items, 0, func(total, item, total + item.cost))
	 * </pre>
	 * In this case, the fact that the "item" field of a item is being accessed
	 * is not detected. Note that "order.items" would be reported as a host
	 * application object reference.
	 *
	 * @return
	 * 	Returns a list containing the host application object identifiers as
	 * 	they appear in the script. For example, if the script contains the
	 * 	identifier `Shopping Cart.Items`, then that exact string is returned
	 * 	(minus the `` characters), not the root object "Shopping Cart". The
	 * 	list returned may be empty.
	 */
	public List<String> references() {
		return references;
	}


	/**
	 * Execute this script within the given environment.
	 *
	 * @param env
	 * 	An object containing the execution environment, including the context,
	 * 	configuration and output stream. The environment given cannot be null.
	 *
	 * @return
	 * 	Returns an Axil object resulting from the execution. The object returned
	 * 	is never null.
	 *
	 * @throws axil.api.error.AxilException
	 * 	This exception is thrown if there are any runtime exceptions in the
	 * 	execution of the script. The exception contains suitable localization
	 * 	to allow the exception to be presented to the user.
	 */
	public AxilObject eval(Environment env) throws AxilException {
		return code.eval(env, StackFrame.root);
	}
}
